﻿//using Emgu.CV;
//using Emgu.CV.CvEnum;
//using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RSALicCore
{
    /// <summary>
    /// 二维码定位器
    /// </summary>
    public class QRCodeLocater
    {
        //public static Rectangle[] Locate(Image qrImg)
        //{
        //    // 载入图像
        //    Bitmap barcodeBitmap = new Bitmap(qrImg);
        //    Image<Bgr, byte> img_src = new Image<Bgr, byte>(barcodeBitmap);

        //    // 转灰度图像
        //    Image<Gray, byte> imput_gray = new Image<Gray, byte>(img_src.Size);
        //    CvInvoke.CvtColor(img_src, imput_gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
        //    //CvInvoke.cvCvtColor(img_src, imput_gray, COLOR_CONVERSION.BGR2GRAY);

        //    // 计算x,y方向梯度，相加
        //    Image<Gray, byte> grad_x1 = new Image<Gray, byte>(img_src.Size);
        //    Image<Gray, byte> grad_y1 = new Image<Gray, byte>(img_src.Size);
        //    Image<Gray, byte> grad_all = new Image<Gray, byte>(img_src.Size);
        //    CvInvoke.Sobel(imput_gray, grad_x1, DepthType.Cv8U, 1, 3);
        //    //CvInvoke.cvSobel(imput_gray, grad_x1, 0, 1, 3);
        //    CvInvoke.Sobel(imput_gray, grad_y1, DepthType.Cv8S, 0, 3);
        //    //CvInvoke.cvSobel(imput_gray, grad_y1, 1, 0, 3);
        //    CvInvoke.Add(grad_x1, grad_y1, grad_all);
        //    //CvInvoke.cvAdd(grad_x1, grad_y1, grad_all, IntPtr.Zero);

        //    ////平均模糊(9*9内核大小)
        //    //CvInvoke.cvSmooth(grad_all, grad_all, SMOOTH_TYPE.CV_BLUR, 9, 9, 0, 0);
        //    //CvInvoke.cvShowImage("平均模糊", grad_all);

        //    // 二值化
        //    CvInvoke.Threshold(grad_all, grad_all, 100, 255, ThresholdType.Binary);

        //    // 消除裂缝
        //    Mat element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(21, 21), new Point(0, 0));
        //    // StructuringElementEx element = new StructuringElementEx(21, 21, 0, 0, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
        //    CvInvoke.MorphologyEx(grad_all, grad_all, MorphOp.Close, element, new Point(1, 1), 1, BorderType.Default, new MCvScalar(255, 0, 0, 255));
        //    // CvInvoke.cvMorphologyEx( grad_all, grad_all, IntPtr.Zero, element, CV_MORPH_OP.CV_MOP_CLOSE, 1 );

        //    // 膨胀与腐蚀(消除杂点)
        //    // StructuringElementEx element1 = new StructuringElementEx(5, 5, 0, 0, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
        //    Mat element1 = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(5, 5), new Point(0, 0));
        //    CvInvoke.Erode(grad_all, grad_all, element1, new Point(1, 1), 4, BorderType.Default, new MCvScalar(255, 0, 0, 255));
        //    CvInvoke.Dilate(grad_all, grad_all, element1, new Point(1, 1), 4, BorderType.Default, new MCvScalar(255, 0, 0, 255));

        //    // 查找轮廓
        //    IntPtr mem_storage = CvInvoke.CreateMemStorage(0);
        //    MCvSeq first_contour = new MCvSeq();
        //    IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址
        //    CvInvoke.FindContours(grad_all, mem_storage, ref Dyncontour,
        //        StructSize.MCvContour,
        //        Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
        //        Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,
        //        new Point(0, 0));
        //    IntPtr temp = first_contour.ptr;
        //    Seq<Point> DyncontourTemp1 = new Seq<Point>(Dyncontour, null);//方便对IntPtr类型进行操作
        //    Seq<Point> DyncontourTemp = DyncontourTemp1;
        //    for (; DyncontourTemp1 != null && DyncontourTemp1.Ptr.ToInt32() != 0; DyncontourTemp1 = DyncontourTemp1.HNext)
        //    {
        //        Rectangle rect = CvInvoke.cvBoundingRect(DyncontourTemp1, true);
        //        if (rect.Width > 20 && rect.Height > 20)
        //        {
        //            rect.Width += 50;
        //            rect.Height += 50;
        //            Point p1 = new Point(rect.X, rect.Y);
        //            Point p2 = new Point(rect.X + rect.Width, rect.Y + rect.Height);
        //            CvInvoke.cvRectangle(img_src, p1, p2, new MCvScalar(0, 0, 255), 2, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0);
        //        }
        //    }


        //}
    }
}
